#define DBC_CHECK_PRECONDITION

using System;
using System.Xml;

using DesignByContract;

namespace Outcoding.GraphGenerator.Entities.XmlBuilders {
    public class ShapeXmlBuilder : IXmlBuilder {

        private Shape mResultObject = null;

        public Shape ResultObject {
            get {
                return mResultObject;
            }
        }

        public ShapeXmlBuilder(XmlNode shapeXmlNode) {
            Check.Require(shapeXmlNode != null, "Xml source node cannot be null");
            this.BuildObject(shapeXmlNode);
        }

        private void BuildObject(XmlNode shapeXmlNode) {
            mResultObject = new Shape();
            try {
                if (shapeXmlNode.Attributes["index"] != null)
                    mResultObject.Index = ParseIndexAttribute(shapeXmlNode);
                else
                    mResultObject.Index = Int32.MinValue;   //used to distinguish a valid index
            } catch (DesignByContract.PreconditionException preExc) {
                //log error
                mResultObject = null;
                throw;
            }
        }

        private int ParseIndexAttribute(XmlNode shapeXmlNode) {
            Check.Require(shapeXmlNode.Attributes["index"] != null, "index attribute not present");
            return Int32.Parse(shapeXmlNode.Attributes["index"].Value);
        }
    }
}
